package com.my.customtask;

import org.eclipse.bpmn2.TextAnnotation;
import org.eclipse.bpmn2.modeler.core.merrimac.clad.AbstractBpmn2PropertySection;
import org.eclipse.bpmn2.modeler.core.merrimac.clad.AbstractDetailComposite;
import org.eclipse.bpmn2.modeler.core.merrimac.clad.DefaultDetailComposite;
import org.eclipse.bpmn2.modeler.core.merrimac.clad.DefaultPropertySection;
import org.eclipse.bpmn2.modeler.core.utils.ModelUtil;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPart;

public class MyTaskPropertySection extends DefaultPropertySection {

   @Override
   protected AbstractDetailComposite createSectionRoot() {
      // here we only want to render specific attributes - override the default behavior
      // which is to display everything, including the TextAnnotation "text" attribute.
      DefaultDetailComposite composite = new MyTaskDetailComposite(this);
      setProperties(composite, new String[] {
            "names", "finish","role" });
      return composite;
   }

   @Override
   public AbstractDetailComposite createSectionRoot(Composite parent, int style) {
      DefaultDetailComposite composite = new MyTaskDetailComposite(parent, style);
      setProperties(composite, new String[] {
            "names", "finish","role"  });
      return composite;
   }

   @Override
   public boolean appliesTo(IWorkbenchPart part, ISelection selection) {
      if (super.appliesTo(part, selection)) {
         EObject eObj = this.getBusinessObjectForSelection(selection);
         if (eObj instanceof TextAnnotation) {
            EStructuralFeature f = ModelUtil.getAnyAttribute(eObj, "names");
            return f != null;
         }
      }
      return false;
   }

   public class MyTaskDetailComposite extends DefaultDetailComposite {

      /**
       * @param section
       */
      public MyTaskDetailComposite(AbstractBpmn2PropertySection section) {
         super(section);
      }

      public MyTaskDetailComposite(Composite parent, int style) {
         super(parent, style);
      }

      @Override
      public void resourceSetChanged(ResourceSetChangeEvent event) {
         super.resourceSetChanged(event);
         System.out.println(event.getSource().getClass());
         // update the TextAnnotation "text" attribute to reflect some changes in
         // our extension attributes; "name" and "cost" in this case.
         final TextAnnotation ta = (TextAnnotation) getBusinessObject();
         final EStructuralFeature nameAttribute = ModelUtil.getAnyAttribute(ta, "names");
         final EStructuralFeature roleAttribute = ModelUtil.getAnyAttribute(ta, "role");
         final TransactionalEditingDomain editingDomain = getDiagramEditor().getEditingDomain();
         for (Notification n : event.getNotifications()) {
            int et = n.getEventType();
            if (et == Notification.SET) {
               if (n.getFeature() == nameAttribute || n.getFeature() == roleAttribute) {
                  // run this in the UI thread
                  Display.getDefault().asyncExec(new Runnable() {

                     public void run() {
                        editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) {

                           @Override
                           protected void doExecute() {
                              String text = new String("name : ");
                              text +=      (String) ta.eGet(nameAttribute);
//                              text += "\n";
//                              text += "subject: ";
//                              text += ta.eGet(roleAttribute).toString();
                              ta.setText(text);
                              if(ta.eGet(roleAttribute)!=null){
                                 System.out.println(ta.eGet(roleAttribute).getClass().toString());
                              }
                           }
                        });
                     }
                  });
               }
            }
         }
      }
   }
}
